<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Keu_kurs extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('combo_model', 'Combo');
		$this->load->model('keu_kurs_model', 'ExchangeRate');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Exchange Rate";
		
		$data['arr_currency']	= $this->Combo->SWValuta();
		
		viewByTemplate('keu_kurs/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->ExchangeRate->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");

		
		// Validation
		if (post("kd_valuta") == '') {
			$error->Add("kd_valuta", "Valuta", $tpl_empty);
		}else if (post("tgl_efektif") == '') {
			$error->Add("tgl_efektif", "Tanggal Efektif", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM KEU_KURS WHERE KD_VALUTA = '".post('kd_valuta')."' AND TGL_EFEKTIF = TO_DATE('".date_format3(post("tgl_efektif"),'YYYY-MM-DD')."', 'YYYY-MM-DD')")) > 0) {
			$error->Add("tgl_efektif", "Tanggal Efektif", $tpl_duplicate);
		} 

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";
		
		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('kd_valuta', 'tgl_efektif', 'harga_beli', 'harga_jual', 'harga_rata'));
				if($data['tgl_efektif']==''){ unset($data['tgl_efektif']); }else{ $data['tgl_efektif']=date_format3($data['tgl_efektif'],'YYYY-MM-DD'); }
				$data['harga_beli']	= toNumber($data['harga_beli']);
				$data['harga_jual']	= toNumber($data['harga_jual']);
				$data['harga_rata']	= toNumber($data['harga_rata']);
				
				if (post("mode") == "create")
				{
					$data['CREATE_DATE']	= NOW;
					$data['CREATE_USER']	= USERID;

					$this->ExchangeRate->Add($data);

					$tpl_insert_success = $this->MessageTemplate("CS001");
					$ls_response = $tpl_insert_success['message_id'];

				}
				else if (post("mode") == "edit")
				{
					$data['MODIFY_DATE']	= NOW;
					$data['MODIFY_USER']	= USERID;
					
					$this->ExchangeRate->Update($data, 
											array(	'KD_VALUTA'	=> $data['kd_valuta'],
													'TGL_EFEKTIF'		=> $data['tgl_efektif'] ));
					
					$tpl_update_success = $this->MessageTemplate("CS002");
					$ls_response = $tpl_update_success['message_id'];

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$kd_valuta	= get('kd_valuta');
		$tgl_efektif		= get('tgl_efektif');
		
		if ($kd_valuta != "")
		{
			// Delete Single
			$ls_result = $this->ExchangeRate->DeleteSingle($kd_valuta, $tgl_efektif);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				$ls_result .= $this->ExchangeRate->DeleteSingle($row['kd_valuta'], $row['tgl_efektif']);
			}
		}
		
		echo $ls_result;
	}
}
?>